package com.kaluli.project.service;

import java.io.IOException;
import java.util.Iterator;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.kaluli.project.domain.entity.ZZStockRequest;
import com.kaluli.project.domain.entity.ZZStockResponse;

@Service
@Configuration
@PropertySource({"classpath:thridsystem.properties"})
public class KllZZOpService extends CommonService{
	
	@Autowired
	private RestClient client;
	
	@Autowired
	private Environment env;
	
	public String getZZOpStock() throws IOException{
		
		ZZStockRequest req = new ZZStockRequest();
		req.setAuthorizationCode(env.getProperty("zz.authorization_code"));
		req.setCustomerId(env.getProperty("zz.customerId"));
		req.setElectricCode(env.getProperty("zz.electriccode"));
		req.setGoodsId("*");
		req.setWarehouseId("10");//仓库
		
		ZZStockResponse res = client.post(env.getProperty("zz.stock.url"), JSON.toJSONString(req), ZZStockResponse.class);
		
		return JSON.toJSONString(res);
		
	}
	public String getNums(String nums){
		if(StringUtils.isBlank(nums)){
			return "";
		}
		int base[]= new int[5];
		Random random = new Random();
		String[] baseStr =nums.split(",");
		for(int n=0;n<baseStr.length;n++){
			base[n]=Integer.valueOf(baseStr[n]);
		}
		
		//万千求和
		StringBuffer bf = new StringBuffer();
		for(int i=0;i<10;i++){
			
			for(int j=0;j<10;j++){
//				j = getNumj(i,j);
				if(i+j == base[0] + base[1]){
					continue;
				}
				for(int k =0;k<10;k++){
					if(i+j+k == base[0] + base[1]+base[2]){
						continue;
					}
					for(int a =0;a<10;a++){
						if(i+j+k+a == base[0] + base[1]+base[2]+base[3]){
							continue;
						}
//						System.out.println(i+"-"+j+"-"+k+"-"+a);
						if(j+k+a ==base[1]+base[2]+base[3]){
							continue;
						}
						if(k+a ==base[2]+base[3]){
							continue;
						}
						for(int b =0;b<10;b++){
							if(a+b ==base[3]+base[4]){
								continue;
							}
							if(k+a+b ==base[2]+base[3]+base[4]){
								continue;
							}
							if(j+k+a+b ==base[1]+base[2]+base[3]+base[4]){
								continue;
							}
							bf.append(i+"");
							bf.append(j+"");
							bf.append(k+"");
							bf.append(a+"");
							bf.append(b+"");
							bf.append("\t");
						}
						
					}
				}
			}
			
		}
		return bf.toString();
	}
	
	public String getNumsTest(String nums,String type){
		if(StringUtils.isBlank(nums)){
			return "";
		}
		if(StringUtils.isBlank(type)){
			return getNums(nums);
		}
		//计算需要校验的位数
		String typeNum[] = type.split(",");
		int wei[] = new int[5];
		
		for (int i = 0; i < typeNum.length; i++) {
			wei[i]=Integer.valueOf(typeNum[i])-1;
		}
		
		int base[]= new int[5];
		Random random = new Random();
		//获取需要比较的原始数据
		String[] baseStr =nums.split(",");
		for(int n=0;n<baseStr.length;n++){
			base[n]=Integer.valueOf(baseStr[n]);
		}
		
		String index[] ={"i","j","k","a","b"};
		StringBuffer bf = new StringBuffer();
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				for(int k =0;k<10;k++){
					for(int a =0;a<10;a++){
						for(int b =0;b<10;b++){
							
							
							//拼接过滤条件
							StringBuffer con = new StringBuffer();
							for(int m=0;m<wei.length;m++){
								con.append(index[wei[m]]);
								if(m==wei.length-1){
									
								}else{
									con.append("+");
								}
								
							}
							StringBuffer dition = new StringBuffer();
							for(int m=0;m<wei.length;m++){
								con.append("base["+wei[m]+"]");
								if(m==wei.length-1){
									
								}else{
									con.append("+");
								}
								
							}
							
							if(con.toString() == dition.toString()){
								continue;
							}
							bf.append(i+"");
							bf.append(j+"");
							bf.append(k+"");
							bf.append(a+"");
							bf.append(b+"");
							bf.append("\t");
						}
						
					}
				}
			}
			
		}
		return bf.toString();
	}
	
}
